package gui;

import java.awt.Color;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import modell.Mitarbeiter;
import modell.MyListModell;

public class DynList extends JPanel implements ChangeListener,ListSelectionListener {

	/**
	 * @author Joachim
	 * @use Creates a jlist for the gui. To display the available workers.
	 * @param Kadugui, DefaultListModel
	 */
	public DefaultListModel model,tmp,tmp2;
	public MyListModell myListModell;
	public JList jlist1;
	public KaduGui kadugui;
	public Vector<Mitarbeiter> vec;

	public DynList(KaduGui kadugui, DefaultListModel model){
		this.kadugui = kadugui;
		this.model = model;

		setLayout(null);
		setSize(200,200);
		setLocation(10,10);
		setBackground(Color.lightGray);
		myListModell = kadugui.kaduController.kadumodell.getModell();
		jlist1 = new JList(myListModell.dfm);
		jlist1.setBorder(null);
		jlist1.addListSelectionListener(this);
		jlist1.setSize(200,200);
		jlist1.setLocation(1,1);
		jlist1.setBackground(Color.WHITE);
		jlist1.updateUI();
		JScrollPane listScroller = new JScrollPane(jlist1);
		listScroller.setSize(200,200);
		listScroller.setLocation(1,1);;
		listScroller.setVisible(true);
		// adding the scrollpane to the parent panel
		// this is a subclass of JPanel, so i can use 
		// super....
		super.add(listScroller);
		showList();
		setVisible(true);
		this.updateUI();
	}

	public void showList() {

		Vector<Mitarbeiter> vec = kadugui.kaduController.getKadumodell().pool.vector;

		if(!myListModell.isEmpty()) {
			myListModell.removeAllElements();
			for(int i=0; i<vec.size();i++) {
				Mitarbeiter m = vec.get(i);
				myListModell.addElement(m);
			}
		}
		else {
			myListModell.removeAllElements();
			for(int i=0; i<vec.size();i++) {
				Mitarbeiter m = vec.get(i);
				myListModell.addElement(m);	
			}
		}
	}

	private static final long serialVersionUID = 1L;

	@Override
	public void stateChanged(ChangeEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("Programmflusskontrolle: DynList stateChanged()");

	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("Programmflusskontrolle: DynList ValueChanged()");
		System.out.println("selected index was :"  +jlist1.getSelectedIndex());
		System.out.println("selected value was :"  +jlist1.getSelectedValue());

	}
	public void addElementTotheList(String element)	{
		myListModell.addElement(element);
	}

	public void refreshTheData()
	{
		jlist1.setVisible(false);

		showList();

		jlist1.setVisible(true);
	}
	public void clearTheList()
	{
		tmp = kadugui.kaduController.kadumodell.myListModell.getDfm();
		tmp.removeAllElements();
		kadugui.kaduController.kadumodell.myListModell.setDfm(tmp);
	}
	public void removeSelectedElement(){

		System.out.println("removeSelectedElement method");
		int removeMe = jlist1.getSelectedIndex();
		tmp = kadugui.kaduController.kadumodell.myListModell.getDfm();
		tmp2 = tmp;
		if (jlist1.getSelectedIndex()>=0){
			tmp2.removeElementAt(removeMe);
			//tmp.removeAllElements();
			tmp=tmp2;
			//tmp.removeElementAt(removeMe);
			kadugui.kaduController.kadumodell.myListModell.setDfm(tmp);
			jlist1.clearSelection();
		}
	}
}
